package com.lgx.mall.business.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;


import net.sf.json.JSONObject;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.entity.IdGenerator;
import com.lgx.common.web.util.DateUtils;
import com.lgx.common.web.util.DictUtils;

import com.lgx.right.entity.User;

/**
 * 订单
 * 
 * @author hezhuo
 *
 */
@Entity
@Table(name = "TM_ORDER")
public class Order implements Serializable {

	private static final long serialVersionUID = 9038811823382270127L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator=IdGenerator.TM_ORDER_ID)
	@TableGenerator(
			name = IdGenerator.TM_ORDER_ID,
			table = IdGenerator.GENERATOR_TABLE,
			pkColumnName = IdGenerator.GEN_NAME,
			pkColumnValue = IdGenerator.TM_ORDER_ID,
			valueColumnName = IdGenerator.GEN_VALUE,
			initialValue = 1,
			allocationSize = 1
	)
	private long id;  //此id为与订单明细信息表作为主外键关联使用
	
	/**
	 * 支付宝交易号
	 */
	private String tradeNo;

	/**
	 * 订单号
	 * 当订单的status由0（为0是购物车状态）变为1（已经生成订单了）时创建订单号
	 */
	private String orderNo;
	
	/**
	 * 购物车放入时间
	 */
	private Date shoppingTime;
	
	/**
	 * 成交时间
	 */
	private Date concludeTime;
	
	/**
	 * 付款时间
	 */
	private Date payTime;
	
	/**
	 * 发货时间
	 * 
	 */
	private Date shipTime;
	
	/**
	 * 确认时间
	 */
	private Date confirmTime;
	
	/**
	 * 订单状态（0购物车1提交2付款3发货4确认（流程走完）5过期（流程走完）6取消订单（流程走完）7申请退货8客服确认退货申请9客服拒绝退货申请（流程走完）10退货成功（流程走完）11缺货（流程走完））
	 * 付款，如果配送方式为自取不需要有付款选择。
	 * 如果是货到付邮，则付款状态不需要被添加，在送货人确定到接受到邮费后，直接修改为完成
	 */
	private int status = 0;
	
	/**
	 * 所属用户
	 * 根据用户标示到会员卡号
	 */
	@ManyToOne
	@JoinColumn(name = "user_id")
	private User user;
	
	/**
	 * 收货人姓名
	 */
	private String consigneeName;
	
	/**
	 * 收货人电话
	 */
	private String consigneeTel;
	
	/**
	 * 邮政编码
	 */
	private String zipcode;
	
	/**
	 * 所属省份
	 */
	private String province;
	
	/**
	 * 所属市区
	 */
	private String city;
	
	/**
	 * 所属区域
	 */
	private String region;
	
	/**
	 * 街道地址
	 */
	private String address;
	
	/**
	 * 运费支付方式
	 * 特快专递，支付宝支付
	 * 货到付邮
	 * 自取
	 */
	private String fareway;
	
	/**
	 * 运费
	 */
	private Double fare;
	
	/**
	 * 订单积分总额
	 */
	private Double score;
	
	/**
	 * 订单商品总数
	 */
	private int amount;

	/**
	 * 快递公司
	 */
	private String expcom;
	
	/**
	 * 快递单号
	 */
	private String expNo;
	
	/**
	 * 快递下单时间
	 */
	private String expDate;
	
	/**
	 * 备注
	 */
	private String remarks;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getTradeNo() {
		return tradeNo;
	}

	public void setTradeNo(String tradeNo) {
		this.tradeNo = tradeNo;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public Date getShoppingTime() {
		return shoppingTime;
	}

	public void setShoppingTime(Date shoppingTime) {
		this.shoppingTime = shoppingTime;
	}

	public Date getConcludeTime() {
		return concludeTime;
	}

	public void setConcludeTime(Date concludeTime) {
		this.concludeTime = concludeTime;
	}

	public Date getPayTime() {
		return payTime;
	}

	public void setPayTime(Date payTime) {
		this.payTime = payTime;
	}

	public Date getShipTime() {
		return shipTime;
	}

	public void setShipTime(Date shipTime) {
		this.shipTime = shipTime;
	}

	public Date getConfirmTime() {
		return confirmTime;
	}

	public void setConfirmTime(Date confirmTime) {
		this.confirmTime = confirmTime;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getConsigneeName() {
		return consigneeName;
	}

	public void setConsigneeName(String consigneeName) {
		this.consigneeName = consigneeName;
	}

	public String getConsigneeTel() {
		return consigneeTel;
	}

	public void setConsigneeTel(String consigneeTel) {
		this.consigneeTel = consigneeTel;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getFareway() {
		return fareway;
	}

	public void setFareway(String fareway) {
		this.fareway = fareway;
	}

	public Double getFare() {
		return fare;
	}

	public void setFare(Double fare) {
		this.fare = fare;
	}

	public Double getScore() {
		return score;
	}

	public void setScore(Double score) {
		this.score = score;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public String getExpcom() {
		return expcom;
	}

	public void setExpcom(String expcom) {
		this.expcom = expcom;
	}

	public String getExpNo() {
		return expNo;
	}

	public void setExpNo(String expNo) {
		this.expNo = expNo;
	}

	public String getExpDate() {
		return expDate;
	}

	public void setExpDate(String expDate) {
		this.expDate = expDate;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
	public Dictionary fareWays(){
		return DictUtils.getDictByKindAndCode(DictUtils.PRODUCT_FAREWAY, getFareway());
	}
	
	/*public String shoppingTime(){
		return DateUtils.dateToChar(getShoppingTime(), DateUtils.DATE_FORMAT_T);
	}*/
	
	public String concludeTime(){
		return DateUtils.dateToChar(getConcludeTime(), DateUtils.DATE_FORMAT_T);
	}
	
	/*public String confirmTime(){
		return DateUtils.dateToChar(getConfirmTime(), DateUtils.DATE_FORMAT_T);
	}*/
	
	/*public String payTime(){
		return DateUtils.dateToChar(getPayTime(), DateUtils.DATE_FORMAT_T);
	}*/
	
	/*public String shipTime(){
		return DateUtils.dateToChar(getShipTime(), DateUtils.DATE_FORMAT_T);
	}*/
	
	/**
	 * 查看订单状态
	 * 前台展示
	 * @return
	 */
	public String selectStatus(){
		String status = getStatus()+"";
		Dictionary status1 = DictUtils.getDictByKindAndCode(DictUtils.ORDER_TYPE, status);
		status = status1.getName();
		return status;
	}
	
	
	/**
	 * 查看订单状态
	 * 后台展示
	 * @return
	 */
	public String backstageStatus(){
		String status = getStatus()+"";
		Dictionary status1 = DictUtils.getDictByKindAndCode(DictUtils.ORDER_TYPE, status);
		status = status1.getName();
		if("1".equals(status1.getCode())&&getPayTime()==null&&"1".equals(getFareway())){  //已提交 ,但是没有写付款时间而且支付方式为支付宝支付
			status = status1.getName()+" 等待支付";
		}
		return status;
	}
	
	/**
	 * json抛出的订单号的超级链接
	 */
	public String orderNoUrl(){
		return "<a href='javascript:void(0);' onclick='findOrder("+getOrderNo()+")'>"+getOrderNo()+"</a>";
	}
	
	

	/**
	 * 后台抛出数据方法
	 */
	@Override 
	public String toString() {
		JSONObject obj = new JSONObject();
		
		obj.put("id", this.getId());                  //订单编号
		obj.put("orderNo", orderNoUrl());             //订单号
		obj.put("concludeTime",this.getConcludeTime() != null?DateUtils.dateToChar(this.getConcludeTime(), DateUtils.DATE_FORMAT_D):"");      //成交时间
		obj.put("status", backstageStatus());         //订单状态
		obj.put("cardid", this.getUser().getCard().getId()); 	//会员卡号
		obj.put("score", this.getScore());                      //订单需要的积分总额
		obj.put("consigneeName", this.getConsigneeName());      //收货人姓名
		obj.put("consigneeTel", this.getConsigneeTel());        //收货人电话
		obj.put("fare", this.getFare());              	        //运费
		
		return obj.toString();
	}
}
